from urllib.request  import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener

from urllib.error import URLError

username = "UserName"
password="password"

url='http://localhost:5000'

p = HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, url, username, password)

auth_handler = HTTPBasicAuthHandler(p)

opener = build_opener(auth_handler)

try:
    result = opener.open(url)
    if __name__ == '__main__':
        html = result.read().decode('utf-8')
        print(html)
except URLError as e:
    print(e.reason)
